package mainpackage;

public class Rider {
	private String name;
	private String code;
	private boolean onMission;
	private int[] missions = new int[3];
	private boolean [] missType = new boolean[3];
	private Info info;
	
	public Rider(String name ,String code){
		this.name = name;
		this.code = code;
		onMission = false;
		for(int i=0;i<missions.length;i++){
			missions[i] = 0;
		}
		for(int i=0;i<missType.length;i++){
			missType[i] = false;
		}
		info = new Info();
	}
	
	public Rider(String name ,String code,boolean onMission, int training ,int transport,
			int fall,boolean onTrainMiss,boolean onTransportMiss,boolean onFallMiss){
		this.name = name;
		this.code = code;
		this.onMission = onMission;
		missions[0] = training;
		missions[1] = transport;
		missions[2] = fall;
		missType[0] = onTrainMiss;
		missType[1] = onTransportMiss;
		missType[2] = onFallMiss;
		info = new Info();
		
	}
	
	public String getName(){
		return name;
	}
	
	public String getCode(){
		return code;
	}
	
	public boolean getOnMission(){
		return onMission;
	}
	
	public int[] getMissions(){
		return missions;
	}
	
	public String getDragonColour(){
		return info.getColour(code);
	}
	
	public int getStatus(){
		return info.getStatus(code);
	}

	public void setMission(int missionType){//mission of type int means the index of missions
		if( (missionType==0||missionType==1||missionType==2) && onMission==false 
				&& info.getStatus(code)!=3   ){
			if( missionType==0 && info.getStatus(code)==0){
				onMission = true;
				addMission(missionType);
			}else if( (missionType==1||missionType==2) && (info.getStatus(code)==1||info.getStatus(code)==2) ){
				onMission = true;
				addMission(missionType);
			}else{
				System.out.println("mission type = "+missionType+" onMission = "+onMission
						+" info.getStatus(code) = "+info.getStatus(code));
				System.out.println("conditions can not be met,nothing is changed");
			}
		}else{
			System.out.println("mission type = "+missionType+" onMission = "+onMission
					+" info.getStatus(code) = "+info.getStatus(code));
			System.out.println("conditions can not be met,nothing is changed");
		}
	}
	
	private void addMission(int missionType){
		missions[missionType]++;
		missType[missionType] = true;
	}
	
	public void endMission(){
		if(onMission==true){
			for(int i=0;i<missType.length;i++){
				if(missType[i]==true){
					missType[i]=false;
					break;
				}
			}
		}else{
			System.out.println("The rider is not on mission");
		}
	}
	
	public void endMission(int missionType){
		missType[missionType] = false;
	}
	
	public void updateHours(){
		if(onMission==true){
			if(missType[0]==true){
				code = info.updateHours(this.code, 4);
				if(code.substring(7).equals("00")){
					endMission(0);
				}
			}
			if(missType[1]==true){
				code = info.updateHours(this.code, 6);
				if(code.substring(7).equals("00")){
					endMission(1);
				}
			}
			if(missType[2]==true){
				code = info.updateHours(this.code, 2);
				if(code.substring(7).equals("00")){
					endMission(2);
				}
			}
		}
		
	}
	
	public boolean availForMission(){
		if(onMission==false && info.getStatus(code)!=3 ){
			return true;
		}
		else{
			System.out.println("The rider is not available for mission");
			return false;
		}
	}
	
	public void setStatus( int newStatus ){
		if(newStatus==0 ||newStatus==1 ||newStatus==2 ||newStatus==3){
			code = info.updateStatus(code, newStatus);
		}
		else{
			System.out.println("The new status is not valid");
		}
	}
	public String toString(){
		String missionType ;
		if(missType[0]==true){
			missionType = "training";
		}else if(missType[1]==true){
			missionType = "transport";
		}else if(missType[2]==true){
			missionType = "fall";
		}else{
			missionType = "no mission type";
		}
		return "Rider[ name: "+name+" is "+(info.getGender(code)=='M'?"male":"female")+"\n"
				+"rides "+info.getColour(code)+" dragon "+info.getDragonCode(code)+" is currently "+(onMission==true?"on a mission":"not on a mission")+"\n"
				+"Mission type: Thread "+missionType+", is of status level: "+info.getStatus(code)+"\n"
				+"currently has flown for "+info.getHours(code)+" hours without a break"+"\n"
				+"and has flown a total of \n"+missions[0]+" training missions, "+missions[1]+" transport missions,"+missions[2]+" fall missions ]\n";
	}
	
}
